<?php

namespace App\Http\Validate\CallingCards\InvestmentEmployee;

use App\Http\Validate\AbstractBaseValidate;
use App\Models\FeishuCallingCard\FeishuCallingCardInvestmentEmployeeModel;
use Illuminate\Support\MessageBag;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class InvestmentEmployeeValidate extends AbstractBaseValidate
{
    function getRules(): array
    {
        // TODO: Implement getRules() method.
        return [];
    }

    function getAttributes(): array
    {
        // TODO: Implement getAttributes() method.
        return [];
    }

    function registerValidateRules(): void
    {
        // TODO: Implement registerValidateRules() method.
    }

    function getMessages(): array
    {
        // TODO: Implement getMessages() method.
        return [];
    }

    /**
     * 列表校验
     *
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function listValidate(array $params)
    {
        return self::getInstance()->validate(
            [
                "pageSize",
                "pageNum",
            ],
            $params,
            [
                "pageSize" => "required|int|min:1|max:100",
                "pageNum"  => "required|int|min:1",
            ]
        );
    }

    /**
     * 新增校验
     *
     * @param array $params
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function insertValidate(array $params)
    {
        return self::getInstance()->validate(
            [
                'name',
                'xiaokeUserId',
                'source',
                'interactiveChannel',
                'public',
                'direct',
                'cityCodes',
            ],
            $params,
            [
                "name"                     => self::stringValidateBuild(true, 32, 1),
                "xiaokeUserId"             => "sometimes|required|int|min:0",
                "source"                   => self::stringValidateBuild(false, 64),
                "interactiveChannel"       => self::stringValidateBuild(false, 64),
                "public"                   => self::intInValidateBuild(
                    true, FeishuCallingCardInvestmentEmployeeModel::SELECT_ENUM
                ),
                "direct"                   => self::intInValidateBuild(
                    true, FeishuCallingCardInvestmentEmployeeModel::SELECT_ENUM
                ),
                "city"                     => self::intInValidateBuild(
                    true, FeishuCallingCardInvestmentEmployeeModel::SELECT_ENUM
                ),
                "cityCodes"                => "exclude_unless:city,1|required|array|min:1",
                "cityCodes.*.cityCode"     => "required|int",
                "cityCodes.*.cityName"     => "required|string",
                "cityCodes.*.provinceName" => "required|string",
                "cityCodes.*.provinceCode" => "required|int",
            ],
            true,
            function ($params) {
                if (isset($params["cityCodes"]) && is_array($params["cityCodes"])) {
                    $params["cityCodes"] = json_encode($params["cityCodes"], JSON_UNESCAPED_UNICODE);
                }
                return $params;
            }
        );
    }

    /**
     * 编辑校验
     *
     * @param array $params
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function updateValidate(array $params)
    {
        return self::getInstance()->validate(
            [
                'id',
                'name',
                'xiaokeUserId',
                'source',
                'interactiveChannel',
                'public',
                'direct',
                'cityCodes',
            ],
            $params,
            [
                "id"                       => [
                    "required",
                    "int",
                    Rule::exists((new FeishuCallingCardInvestmentEmployeeModel())->getTable(), "id")
                ],
                "name"                     => self::stringValidateBuild(true, 32, 1),
                "xiaokeUserId"             => "sometimes|required|int|min:1",
                "source"                   => self::stringValidateBuild(false, 64),
                "interactiveChannel"       => self::stringValidateBuild(false, 64),
                "public"                   => self::intInValidateBuild(
                    true, FeishuCallingCardInvestmentEmployeeModel::SELECT_ENUM
                ),
                "direct"                   => self::intInValidateBuild(
                    true, FeishuCallingCardInvestmentEmployeeModel::SELECT_ENUM
                ),
                "city"                     => self::intInValidateBuild(
                    true, FeishuCallingCardInvestmentEmployeeModel::SELECT_ENUM
                ),
                "cityCodes"                => "exclude_unless:city,1|required|array|min:1",
                "cityCodes.*.cityCode"     => "required|int",
                "cityCodes.*.cityName"     => "required|string",
                "cityCodes.*.provinceName" => "required|string",
                "cityCodes.*.provinceCode" => "required|int",
            ],
            true,
            function ($params) {
                if (isset($params["cityCodes"]) && is_array($params["cityCodes"])) {
                    $params["cityCodes"] = json_encode($params["cityCodes"], JSON_UNESCAPED_UNICODE);
                }
                return $params;
            }
        );
    }

    /**
     * 删除校验
     *
     * @param array $params
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function delValidate(array $params)
    {
        return self::getInstance()->validate(
            [
                'id'
            ],
            $params,
            [
                "id" => [
                    "required",
                    "int",
                    Rule::exists((new FeishuCallingCardInvestmentEmployeeModel())->getTable(), "id")
                ]
            ]
        );
    }
}
